package com.yuyou.fn.timer.task;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.AttendClassRecord;
import com.yuyou.fn.educational.service.IAttendClassRecordService;
import com.yuyou.fn.educational.service.ICourseConsumeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 计算课程消耗
 * Created by wens on 2017/4/19.
 */
@JobHander(value="CourseConsumeTask")
@Component
public class CourseConsumeTask extends IJobHandler {

    @Resource
    private IAttendClassRecordService attendClassRecordService ;

    @Resource
    private ICourseConsumeService courseConsumeService;

    @Override
    public ReturnT<String> execute(String... params) throws Exception {
        List<AttendClassRecord> attendClassRecordList = attendClassRecordService.findAttendClassRecordForConsume();
        boolean hasFail  = false ;
        for(AttendClassRecord attendClassRecord : attendClassRecordList ){
            try{
                courseConsumeService.consumeForAttendClassRecord(attendClassRecord);
            }catch (Exception e){
                hasFail = true ;
                XxlJobLogger.log("处理失败:attendClassRecordId={0},error={1}" , attendClassRecord.getClassLectureId() , Utils.getExceptionStackTrace(e));
            }
        }
        return hasFail ? ReturnT.FAIL : ReturnT.SUCCESS ;
    }

    public void execute2(List<AttendClassRecord> attendClassRecordList) {

    }





}
